/**
 ************************************* Copyright ******************************
 *
 *                 (C) Copyright 2024,MegaHunt.Inc,China, GCU.
 *                            All Rights Reserved
 *
 *                     By(nanshou)
 *
 *
 * FileName   : nfc_hal_transfer.c
 * Version    : v1.0
 * Author     : MegaHunt
 * Date       : 2024-07-04
 * Description:
 * Function List:
  	1. ....
  	   <version>:
  <modify staff>:
  		  <data>:
   <description>:
  	2. ...
 ******************************************************************************
 */
#include "nfc_hal_transfer.h"
#include "nfc_hal_config.h"
#include "nfc_hal_dpo.h"
#include <stdio.h>
#include "user_spi_driver.h"
#include "user_nfc_driver.h"
#include "CH58x_common.h"
#include "sys_init.h"

#define FIFO_SIZE	64

pcd_info_s g_pcd_module_info;

extern uint8_t EMV_TEST;
extern uint8_t EMV_DEBUG_LOG;

uint8_t NFC_DEBUG_LOG = 0;

void delay_1ms(uint32_t delay_time){
#ifdef USE_STDPERIPH_DRIVER
    delay_ms(delay_time);
#else
    mDelaymS(delay_time);
#endif
}
__HIGH_CODE
void write_reg(uint8_t addr, uint8_t RegValue) {
#if defined IIC_Mode
    i2c_write_word(SL2x23_DEV_ADDRESS << 1, addr, RegValue);
#elif defined UART_Mode
    addr = addr & 0x3f;      //code the first byte
    uart1_wrreg(addr, RegValue);
#else
#if(NFC_CHIP_TYPE == NFC_CHIP_MH1612S )
    addr = ((addr/* & 0x3f*/) << 1) & 0x7E;     //code the first byte
#else
    addr = (addr << 1) & 0xFE;
#endif
#ifdef USE_STDPERIPH_DRIVER
    DEV_WriteSR(addr, RegValue);
#else
    //接口函数
    DEV_WriteSR(addr, RegValue);
#endif
#endif
}
__HIGH_CODE
uint8_t read_reg(uint8_t addr) {
#if defined IIC_Mode
    uint8_t reg_value;
    i2c_read_word(SL2x23_DEV_ADDRESS << 1, addr, &reg_value);
    return reg_value;
#elif defined UART_Mode
    addr = (addr & 0x3f) | 0x80;    //code the first byte
    uart1_rdreg(addr);
#else
#if(NFC_CHIP_TYPE == NFC_CHIP_MH1612S )
    addr = ((addr /*& 0x3f*/) << 1) | 0x80;     //code the first byte
#else
    addr = (addr << 1) | 0x01;
#endif

#ifdef USE_STDPERIPH_DRIVER
    return DEV_ReadSR(addr);
#endif
    //接口函数
    return DEV_ReadSR(addr);
#endif
}

//void spi_wr_test(void) {
//    int ok = 0;
//    uint8_t err = 0;
//    int i = 0;
//    uint8_t ret = 0xff;
//    pcd_reset();//lpcd模式，下直接更新固件。不发reset，spi测试不成功
//
//    delay_1ms(1);
//
//    uint8_t reg = read_reg(0X0B);
//    printf("reg%x\r\n", reg);
//    for(i = 0; i < 100; i++) {
//        write_reg(0x09, i);
//        ret = read_reg(0x09);
//        if(i != ret) {
//            err++;
//            printf("i = 0x%x,errnum = 0x%x, data=0x%x\r\n", i, err, ret);
//        } else {
//            ok++;
//        }
//    }
//    if(ok == 100) {
//        printf("spi test pass! chip id = 0x%x\r\n",read_reg(0x37));
//    } else {
//        printf("spi test fail!\r\n");
//    }
//
//}

#if USER_PICC_EN
#endif
const hal_driver_interface_t drv_interface = {
        .hw_init            = nfc_spi_init,
        .hw_transceive      = spi_transceive,
        .hw_pin_init        = user_nfc_gpio_init,
        .hw_set_pcd_power   = set_pcd_power,
        .hw_clock_init      = nfc_timer_init,
        .hw_msleep          = mDelaymS,
        .hw_get_tick		= get_tick,
        .hw_is_timeout		= is_timeout,
        //.hw_put_char		= uart_SendChar,
};

unsigned char GetReg(unsigned char address,unsigned char *reg_data){
    *reg_data =  read_reg(address);

    return 1;
}

unsigned char SetReg(unsigned char address,unsigned char reg_data){
    write_reg(address, reg_data);

    return 1;
}
unsigned char FM176XX_HardReset(void)
{
    unsigned char reg_data;
    NSS_1;//NSS = 1
    PD_1;//PD = 1
    DelayMs(1);
    PD_0;//PD = 0
    DelayMs(1);
    GetReg(0x00,&reg_data);
    if (reg_data != 0x40)
        return 0;
    return 1;
}

unsigned char Reader_Set_HPD( unsigned char mode )                                            //mode = DISABLE 退出HPD模式 ，mode = ENABLE 进入HPD模式
{
    if ( mode == ENABLE )
    {
        DelayMs( 1 );                                                       //延时1ms

        PD_1;      // PD = 1

    }
    else
    {

        PD_0;   //PD = 0
        DelayMs( 1 );                                                       //延时1ms，等待Reader启动
    }
    return (mode);
}

void hal_nfc_init(void)
{
    uint8_t nfc_version = 0;
    unsigned char result,reg_data;
    uint8_t cnt = 2;

    drv_interface.hw_clock_init();//空函数
    drv_interface.hw_pin_init();//gpio管脚初始化
    drv_interface.hw_init();//spi 初始化
    drv_interface.hw_set_pcd_power(0);//是否进入低功耗
    drv_interface.hw_msleep(5);

    DelayMs(500);
    printf( "FM15L0XX FM17661 T2T DEMO!\r\n" );
    printf( "FM15L0XX FM17660 DEMO!\r\n" );
    while(cnt--)
    {
        result = FM176XX_HardReset();
        if(result == 0 )
        {
            free_dog();
            printf("FM17660 FAIL\r\n");
            DelayMs(100);
        }
        else
            break;
    }
    GetReg(0x7F, &nfc_version);
    printf( "REG_VERSION = %02X\r\n",nfc_version );


    //    hal_nfc_pcd_reset();
    //    uint8_t reg = hal_nfc_read_register(0X0B);
    //    printf("reg%x\r\n", reg);//读出来为0x08
    //    hal_nfc_antenna_off();
    //    sys_delay_ms(50);
    //    printf("hal_nfc_antenna_off\r\n");
    //    hal_nfc_antenna_on();
    //    sys_delay_ms(10);
    //    hal_nfc_antenna_off();
    //    sys_delay_ms(10);
    //    hal_nfc_antenna_on();

}

void hal_nfc_reinit(void)
{
    hal_nfc_pcd_reset();
}

int hal_nfc_write_register(uint8_t addr, uint8_t value)
{

    write_reg(addr, value);

    return 0;
}


/*
 * @brief hal_nfc_read_register
 *      读NFC芯片寄存器
 *
 * @param: addr 寄存器地址
 *
 * @return: 寄存器的值
 */
uint8_t hal_nfc_read_register(uint8_t addr)
{
    return read_reg(addr);
}

void hal_nfc_set_register_bit(uint8_t addr, uint8_t mask)
{
    uint8_t tmp;

    tmp = hal_nfc_read_register(addr);
    hal_nfc_write_register(addr, tmp | mask);
}

void hal_nfc_clear_register_bit(uint8_t addr, uint8_t mask)
{
    uint8_t tmp;

    tmp = hal_nfc_read_register(addr);
    hal_nfc_write_register(addr, tmp & ~mask);
}

int pcd_com_transceive(hal_nfc_transceive_t *pi)
{

    uint8_t recebyte;
    int status = HAL_STATUS_OK;
    uint8_t irq_en;
    uint8_t wait_for;
    uint8_t last_bits;
    uint8_t j;
    uint8_t val;
    uint8_t err;
    uint16_t  len_rest;
    uint8_t  len;
    uint8_t WATER_LEVEL;

    len = 0;
    len_rest = 0;
    err = 0;
    recebyte = 0;
    irq_en = 0;
    wait_for = 0;
    //    optimum_setting_gsp(RFalDPOParam.AdjustCnt);
    switch (pi->mf_command)
    {
    case PCD_IDLE:
        irq_en   = 0x00;
        wait_for = 0x00;
        break;
    case PCD_AUTHENT:
        irq_en = IDEL_IEN | TIMER_IEN;
        wait_for = IDEL_IRQ;
        break;
    case PCD_RECEIVE:
        irq_en   = RX_IEN | IDEL_IEN;
        wait_for = RX_IRQ;
        recebyte=1;
        break;
    case PCD_TRANSMIT:
        irq_en   = TX_IEN | IDEL_IEN;
        wait_for = TX_IRQ;
        break;
    case PCD_TRANSCEIVE:
        irq_en = RX_IEN | IDEL_IEN | TIMER_IEN | TX_IEN;
        wait_for = RX_IRQ;
        recebyte=1;
        break;
    default:
        pi->mf_command = HAL_STATUS_UNKNOWN_CMD;
        break;
    }

    WATER_LEVEL = hal_nfc_read_register(REG_WATERLEVEL);

    if (pi->mf_command != HAL_STATUS_UNKNOWN_CMD
            && (((pi->mf_command == PCD_TRANSCEIVE || pi->mf_command == PCD_TRANSMIT) && pi->mf_length > 0)
                    || (pi->mf_command != PCD_TRANSCEIVE && pi->mf_command != PCD_TRANSMIT))
    )
    {
        hal_nfc_write_register(REG_COMMAND, PCD_IDLE);
        hal_nfc_write_register(REG_COMIEN, /*irq_inv |*/irq_en | BIT(0));//
        hal_nfc_write_register(REG_COMIRQ, 0x7F); //Clear INT
        hal_nfc_write_register(REG_DIVIRQ, 0x7F); //Clear INT
        //Flush Fifo
        hal_nfc_set_register_bit(REG_FIFOLEVEL, BIT(7));
        if (pi->mf_command == PCD_TRANSCEIVE || pi->mf_command == PCD_TRANSMIT || pi->mf_command == PCD_AUTHENT)
        {
            if(NFC_DEBUG_LOG)
                printf(" PCD_tx:");
            for (j = 0; j < pi->mf_length; j++)
            {

                if(NFC_DEBUG_LOG)
                    printf("%02x ", pi->mf_data[j]);
            }
            if(NFC_DEBUG_LOG)
                printf("\n");
            if(EMV_TEST)
            {
                if(EMV_DEBUG_LOG)
                {
                    printf("C ->: ");
                    for (j = 0; j < pi->mf_length; j++)
                    {
                        if (j > 4)
                        {
                            printf("..");
                            break;
                        }
                        else
                        {
                            printf("%02X ", (uint8_t)pi->mf_data[j]);
                        }
                    }
                    printf("l=%d", pi->mf_length);
                    printf("\n");
                }
            }
            len_rest = pi->mf_length;
            if (len_rest >= FIFO_SIZE)
            {
                len = FIFO_SIZE;
            } else
            {
                len = len_rest;
            }

            for (j = 0; j < len; j++)
            {
                hal_nfc_write_register(REG_FIFODATA, pi->mf_data[j]);
            }
            len_rest -= len;//Rest bytes
            if (len_rest != 0)
            {
                hal_nfc_write_register(REG_COMIRQ, BIT(2)); // clear LoAlertIRq
                hal_nfc_set_register_bit(REG_COMIEN, BIT(2));// enable LoAlertIRq
            }

            hal_nfc_write_register(REG_COMMAND, pi->mf_command);
            if (pi->mf_command == PCD_TRANSCEIVE)
            {
                hal_nfc_set_register_bit(REG_BITFRAMING,0x80);
            }

            while (len_rest != 0)
            {
                while(INT_PIN == 0);//Wait LoAlertIRq
                if (len_rest > (FIFO_SIZE - WATER_LEVEL))
                {
                    len = FIFO_SIZE - WATER_LEVEL;
                }
                else
                {
                    len = len_rest;
                }
                for (j = 0; j < len; j++)
                {
                    hal_nfc_write_register(REG_FIFODATA, pi->mf_data[pi->mf_length - len_rest + j]);
                }

                hal_nfc_write_register(REG_COMIRQ, BIT(2));//

                // printf("\n8 comirq=%02x,ien=%02x,INT= %d \n", (u16)read_reg(ComIrqReg), (u16)read_reg(ComIEnReg), (u16)INT_PIN);
                len_rest -= len;//Rest bytes
                if (len_rest == 0)
                {
                    hal_nfc_clear_register_bit(REG_COMIEN, BIT(2));// disable LoAlertIRq
                    // printf("\n9 comirq=%02x,ien=%02x,INT= %d \n", (u16)read_reg(ComIrqReg), (u16)read_reg(ComIEnReg), (u16)INT_PIN);
                }
            }
            //Wait TxIRq
            while (INT_PIN == 0)
            {
            }

            val = hal_nfc_read_register(REG_COMIRQ);
            if (val & TX_IRQ)
            {
                hal_nfc_write_register(REG_COMIRQ, TX_IRQ);
                if(pi->mf_command == PCD_TRANSMIT)
                {
                    return 0;
                }
            }
        }
        if (PCD_RECEIVE == pi->mf_command)
        {
            hal_nfc_write_register(REG_COMMAND, PCD_RECEIVE);
            hal_nfc_clear_register_bit(REG_TMODE,BIT(7));
            hal_nfc_set_register_bit(REG_CONTROL, BIT(6));// TStartNow
        }

        len_rest = 0; // bytes received
        hal_nfc_write_register(REG_COMIRQ, BIT(3)); // clear HoAlertIRq
        hal_nfc_set_register_bit(REG_COMIEN, BIT(3)); // enable HoAlertIRq



        if(!EMV_TEST)
        {
            while(status != HAL_STATUS_TIMEOUT)
            {
                while(INT_PIN == 0)
                {
                }
                while(1)
                {
                    while(0 == INT_PIN);
                    val = hal_nfc_read_register(REG_COMIRQ);
                    if ((val & BIT(3)) && !(val & BIT(5)))
                    {
                        for (j = 0; j <FIFO_SIZE - WATER_LEVEL; j++)
                        {
                            pi->mf_data[len_rest + j] = hal_nfc_read_register(REG_FIFODATA);
                        }
                        hal_nfc_write_register(REG_COMIRQ, BIT(3));//
                        len_rest += FIFO_SIZE - WATER_LEVEL;
                    }
                    else
                    {
                        hal_nfc_clear_register_bit(REG_COMIEN, BIT(3));//disable HoAlertIRq
                        break;
                    }
                }

                val = hal_nfc_read_register(REG_COMIRQ);
                if(NFC_DEBUG_LOG)
                {
                    printf(" INT:fflvl=%d,rxlst=%02x ,ien=%02x,cirq=%02x\n",
                            hal_nfc_read_register(REG_FIFOLEVEL),
                            (hal_nfc_read_register(REG_CONTROL)&0x07),
                            hal_nfc_read_register(REG_COMIEN), val);//XU
                }

                hal_nfc_write_register(REG_COMIRQ, val);//

                if (val & BIT(0))
                {   //
                    status = HAL_STATUS_TIMEOUT;
                }
                else
                {
                    err = hal_nfc_read_register(REG_ERROR);

                    status = HAL_STATUS_ERR;
                    if ((val & wait_for) && (val & irq_en))
                    {
                        if (!(val & ERR_IRQ))
                        {   //
                            status = HAL_STATUS_OK;

                            if (recebyte)
                            {
                                val = 0x7F & hal_nfc_read_register(REG_FIFOLEVEL);
                                if(hal_nfc_read_register(REG_RXMODE) & BIT(2))
                                    val = val - 1;
                                last_bits = hal_nfc_read_register(REG_CONTROL) & 0x07;

                                if (last_bits && val) //
                                {
                                    pi->mf_length = (val-1)*8 + last_bits;
                                }
                                else
                                {
                                    pi->mf_length = val*8;
                                }
                                pi->mf_length += len_rest*8;

                                if(NFC_DEBUG_LOG)
                                    printf(" RX:len=%02x,dat:", pi->mf_length);

                                if (val == 0)
                                {
                                    val = 1;
                                }
                                for (j = 0; j < val; j++)
                                {
                                    pi->mf_data[len_rest + j] = hal_nfc_read_register(REG_FIFODATA);
                                }

                                if(NFC_DEBUG_LOG)
                                {
                                    for (j = 0; j < pi->mf_length/8 + !!(pi->mf_length%8); j++)
                                    {
                                        printf("%02X ", pi->mf_data[j]);
                                    }
                                    printf("\n");
                                }

                            }
                        }
                        else if (err & COLL_ERR)
                        {
                            printf("COLL ERR\r\n");
                            status = HAL_STATUS_COLLERR;
                            if(pi->type == HAL_TECH_TYPE_A)
                            {
                                //a bit-collision is detected
                                if (recebyte)
                                {
                                    val = 0x7F & hal_nfc_read_register(REG_FIFOLEVEL);
                                    last_bits = hal_nfc_read_register(REG_CONTROL) & 0x07;
                                    if (len_rest + val > MAX_TRX_BUF_SIZE)
                                    {   //
                                        if(NFC_DEBUG_LOG)
                                            printf("COLL RX_LEN > 255B\n");
                                    }
                                    else
                                    {
                                        if (last_bits && val) //
                                        {
                                            pi->mf_length = (val-1)*8 + last_bits;
                                        }
                                        else
                                        {
                                            pi->mf_length = val*8;
                                        }
                                        pi->mf_length += len_rest*8;
                                        if(NFC_DEBUG_LOG)
                                            printf(" RX: pi_cmd=%02x,pi_len=%02x,pi_dat:", pi->mf_command, pi->mf_length);
                                        if (val == 0)
                                        {
                                            val = 1;
                                        }
                                        for (j = 0; j < val; j++)
                                        {
                                            pi->mf_data[len_rest + j +1] = hal_nfc_read_register(REG_FIFODATA);
                                        }
                                        if(NFC_DEBUG_LOG)
                                        {
                                            for (j = 0; j < pi->mf_length/8 + !!(pi->mf_length%8); j++)
                                            {
                                                printf("%02X ", pi->mf_data[j+1]);
                                            }
                                            printf("\n");
                                        }
                                    }
                                }
                                /* The location where the collision occurred is in the first 32 bits, otherwise it is outside the 32 bits */
                                if(!(hal_nfc_read_register(REG_COLL) & BIT(5)))
                                {
                                    pi->mf_data[0] = (hal_nfc_read_register(REG_COLL) & COLLPOS);
                                    if (pi->mf_data[0] == 0)
                                    {
                                        pi->mf_data[0] = 32;
                                    }
                                }
                                else
                                {   //This value is used to tell the upper layer that the conflict position is more than 32 bits
                                    pi->mf_data[0] = 33;
                                }
                                if(NFC_DEBUG_LOG)
                                {
                                    printf("\n COLL_DET pos=%02x\n", pi->mf_data[0]);
                                }

                                pi->mf_data[0]--;//
                            }
                        }
                        else if (err & (PROTOCOL_ERR))
                        {
                            if(NFC_DEBUG_LOG)
                                printf("protocol err=%02x\n", err);
                            status = HAL_STATUS_FRAMINGERR;
                        }
                        else if ((err & (CRC_ERR | PARITY_ERR)) && !(err &PROTOCOL_ERR) )
                        {
                            //EMV  parity err EMV 307.2.3.4
                            val = 0x7F & hal_nfc_read_register(REG_FIFOLEVEL);
                            last_bits = hal_nfc_read_register(REG_CONTROL) & 0x07;
                            if (len_rest + val > MAX_TRX_BUF_SIZE)
                            {
                                status = HAL_STATUS_ERR;
                                if(NFC_DEBUG_LOG)
                                    printf("RX_LEN > 255B\n");
                            }
                            else
                            {
                                if (last_bits && val)
                                {
                                    pi->mf_length = (val-1)*8 + last_bits;
                                }
                                else
                                {
                                    pi->mf_length = val*8;
                                }
                                pi->mf_length += len_rest*8;
                            }
                            if(NFC_DEBUG_LOG)
                            {
                                printf("crc-parity err=%02x\n", err);
                                printf("l=%d\n", pi->mf_length );
                            }

                            status = HAL_STATUS_INTEGRITY_ERR;
                        }
                        else
                        {
                            if(NFC_DEBUG_LOG)
                                printf("unknown ErrorReg=%02x\n", err);
                            status = HAL_STATUS_INTEGRITY_ERR;
                        }
                    }
                    else
                    {
                        status = HAL_STATUS_ERR;
                        if(NFC_DEBUG_LOG)
                            printf(" MI_COM_ERR\n");

                    }
                }

                if(!(hal_nfc_read_register(REG_RXMODE) & BIT(2)))
                {
                    break;
                }
                else if((status == HAL_STATUS_TIMEOUT) && (pi->mf_length != 0))
                {
                    status = HAL_STATUS_OK;
                    break;
                }
                else if((hal_nfc_read_register(REG_RXMODE) & BIT(2)))
                {
                    hal_nfc_write_register(REG_FIFOLEVEL, BIT(7));//Flush Fifo
                }
            }
        } //!emv_test end

        else
        {   //emv_test start
            while(1)
            {
                while(0 == INT_PIN);
                val = hal_nfc_read_register(REG_COMIRQ);
                if ((val & BIT(3)) && !(val & BIT(5)))
                {
                    for (j = 0; j <FIFO_SIZE - WATER_LEVEL; j++)
                    {
                        pi->mf_data[len_rest + j] = hal_nfc_read_register(REG_FIFODATA);
                    }
                    hal_nfc_write_register(REG_COMIRQ, BIT(3));//
                    len_rest += FIFO_SIZE - WATER_LEVEL;
                    //if emd enable,timer must stop
                    if(hal_nfc_bit_check(1, 0x18, 1, 1))
                        hal_nfc_bit_set(0, 0x0c, 0x80, 0);
                }
                else
                {
                    hal_nfc_clear_register_bit(REG_COMIEN, BIT(3));//disable HoAlertIRq
                    break;
                }
            }
            val = hal_nfc_read_register(REG_COMIRQ);
            hal_nfc_write_register(REG_COMIRQ, val);//

            if (val & BIT(0))
            {   //
                status = HAL_STATUS_TIMEOUT;
            }
            else
            {
                err = hal_nfc_read_register(REG_ERROR);

                status = HAL_STATUS_ERR;
                if ((val & wait_for) && (val & irq_en))
                {
                    if (!(val & ERR_IRQ))
                    {   //
                        status = HAL_STATUS_OK;

                        if (recebyte)
                        {
                            val = 0x7F & hal_nfc_read_register(REG_FIFOLEVEL);
                            if(hal_nfc_read_register(REG_RXMODE) & BIT(2))
                                val = val - 1;
                            last_bits = hal_nfc_read_register(REG_CONTROL) & 0x07;
                            if (last_bits && val) //
                            {
                                pi->mf_length = (val-1)*8 + last_bits;
                            }
                            else
                            {
                                pi->mf_length = val*8;
                            }
                            pi->mf_length += len_rest*8;


                            if (val == 0)
                            {
                                val = 1;
                            }
                            for (j = 0; j < val; j++)
                            {
                                pi->mf_data[len_rest + j] = hal_nfc_read_register(REG_FIFODATA);
                            }
                            if(EMV_DEBUG_LOG)
                            {
                                printf("R <-: ");
                                for (j = 0; j < pi->mf_length/8 + !!(pi->mf_length%8); j++)
                                {
                                    if (j > 4)
                                    {
                                        printf("..");
                                        break;
                                    }
                                    else
                                    {
                                        printf("%02X ", (uint8_t)pi->mf_data[j]);
                                    }

                                }
                                printf("l=%d", pi->mf_length/8 + !!(pi->mf_length%8));
                                printf("\n");
                            }
                        }//receivebyte end
                    }
                    else if (err & COLL_ERR)
                    {
                        status = HAL_STATUS_COLLERR;
                        if(pi->type == HAL_TECH_TYPE_A)
                        {
                            //a bit-collision is detected
                            if (recebyte)
                            {
                                val = 0x7F & hal_nfc_read_register(REG_FIFOLEVEL);
                                last_bits = hal_nfc_read_register(REG_CONTROL) & 0x07;
                                if (last_bits && val) //
                                {
                                    pi->mf_length = (val-1)*8 + last_bits;
                                }
                                else
                                {
                                    pi->mf_length = val*8;
                                }
                                pi->mf_length += len_rest*8;
                                if (val == 0)
                                {
                                    val = 1;
                                }
                                for (j = 0; j < val; j++)
                                {
                                    pi->mf_data[len_rest + j +1] = hal_nfc_read_register(REG_FIFODATA);
                                }
                            }
                            /* The location where the collision occurred is in the first 32 bits, otherwise it is outside the 32 bits */
                            if(!(hal_nfc_read_register(REG_COLL) & BIT(5)))
                            {
                                pi->mf_data[0] = (hal_nfc_read_register(REG_COLL) & COLLPOS);
                                if (pi->mf_data[0] == 0)
                                {
                                    pi->mf_data[0] = 32;
                                }
                            }
                            else
                            {   //This value is used to tell the upper layer that the conflict position is more than 32 bits
                                pi->mf_data[0] = 33;
                            }
                            pi->mf_data[0]--;//
                        }
                        else if(pi->type == HAL_TECH_TYPE_B)
                        {
                        }
                    }
                    else if (err & (PROTOCOL_ERR))
                    {
                        status = HAL_STATUS_FRAMINGERR;
                    }
                    else if ((err & (CRC_ERR | PARITY_ERR)) && !(err &PROTOCOL_ERR) )
                    {
                        //EMV  parity err EMV 307.2.3.4
                        val = 0x7F & hal_nfc_read_register(REG_FIFOLEVEL);
                        last_bits = hal_nfc_read_register(REG_CONTROL) & 0x07;
                        if (last_bits && val)
                        {
                            pi->mf_length = (val-1)*8 + last_bits;
                        }
                        else
                        {
                            pi->mf_length = val*8;
                        }
                        pi->mf_length += len_rest*8;

                        status = HAL_STATUS_INTEGRITY_ERR;
                    }
                    else
                    {
                        status = HAL_STATUS_INTEGRITY_ERR;
                    }
                }
                else
                {
                    status = HAL_STATUS_ERR;
                }
            }
        }// eme_test end
        hal_nfc_set_register_bit(REG_CONTROL, BIT(7));
        hal_nfc_write_register(REG_COMIRQ, 0x7F);
        hal_nfc_write_register(REG_DIVIRQ, 0x7F);
        hal_nfc_clear_register_bit(REG_COMIEN, 0x7F);
        hal_nfc_clear_register_bit(REG_DIVIEN, 0x7F);
        hal_nfc_write_register(REG_COMMAND, PCD_IDLE);

    }//all command if
    else //all command if 's else
    {
        status = HAL_STATUS_USER_ERR;
        if(NFC_DEBUG_LOG)
            printf("USER_ERROR\n");
    }
    if(NFC_DEBUG_LOG)
        printf(" pcd_com: sta=%d,err=%02x\n", status, err);
    return status;
}

void pcd_default_info(void)// COS_TEST
{
    memset(&g_pcd_module_info, 0, sizeof(g_pcd_module_info));
    g_pcd_module_info.ui_fsc = 32;
    g_pcd_module_info.ui_fwi = 4;
    g_pcd_module_info.ui_sfgi = 0;
    g_pcd_module_info.uc_nad_en = 0;
    g_pcd_module_info.uc_cid_en = 0;
    g_pcd_module_info.uc_wtxm = 1; //multi

}
